package com.rykj.widget.sidebar;

import net.sourceforge.pinyin4j.PinyinHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by hdl on 2017/7/10.
 */

public class PinYinFirstLetter {

    private static final String TAG = "PinYinFirstLetter";

    /**
     * 根据昵称拼音首字母排序
     */
    public static List<IPinyinSort> alphabeticalOrder(List<IPinyinSort> cityInfos) {
        //给数据添加拼音标识
        for (IPinyinSort cityInfo : cityInfos) {
            cityInfo.setPinYinFirstLetter(getPinYinFirstLetter(cityInfo.getPojoSortStr()));
        }
        //把数据按字母排序
        Collections.sort(cityInfos, new Comparator<IPinyinSort>() {
            @Override
            public int compare(IPinyinSort o1, IPinyinSort o2) {
                return ((o1.getPinYinFirstLetter()).compareTo(o2.getPinYinFirstLetter()));
            }
        });
        //通过前后比较拼音首字母是否相同,给排序后的城市列表添加首字母分组
        List<IPinyinSort> tempList = new ArrayList<>();
        if (!cityInfos.isEmpty()) {
            for (int i = 0, size = cityInfos.size(); i < size; i++) {
                if (i == 0) {
                    IPinyinSort group = new PinyinGroup(cityInfos.get(i).getPinYinFirstLetter());
                    tempList.add(group);
                    tempList.add(cityInfos.get(i));

                } else if (i > 0 && i < size - 1) {
                    IPinyinSort cityl = cityInfos.get(i - 1);
                    IPinyinSort city = cityInfos.get(i);
                    IPinyinSort cityN = cityInfos.get(i + 1);
                    boolean t2 = city.getPinYinFirstLetter().equals(cityN.getPinYinFirstLetter());//与下面组名是否相同
                    boolean t1 = city.getPinYinFirstLetter().equals(cityl.getPinYinFirstLetter());//与前面组名是否相同
                    if (!t1 && !t2) {
                        IPinyinSort group = new PinyinGroup(city.getPinYinFirstLetter());
                        tempList.add(group);
                    } else if (!t1 && t2) {
                        IPinyinSort group = new PinyinGroup(city.getPinYinFirstLetter());
                        tempList.add(group);
                    }
                    tempList.add(cityInfos.get(i));

                } else if (i > 0 && i == size - 1) {
                    IPinyinSort cityl = cityInfos.get(i - 1);
                    IPinyinSort city = cityInfos.get(i);
                    boolean t1 = city.getPinYinFirstLetter().equals(cityl.getPinYinFirstLetter());
                    if (!t1) {
                        IPinyinSort group = new PinyinGroup(city.getPinYinFirstLetter());
                        tempList.add(group);
                    }
                    tempList.add(cityInfos.get(i));
                }

            }
        }
        return tempList;
    }

    /**
     * 获取汉字字符串的第一个字母
     */
    private static String getPinYinFirstLetter(String str) {
        StringBuffer sb = new StringBuffer();
        char c = str.charAt(0);
        String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
        if (pinyinArray != null) {
            sb.append(pinyinArray[0].charAt(0));
        } else {
            sb.append(c);
        }
        return sb.toString().toUpperCase();
    }

}
